package com.gy.smart.life;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.amap.api.location.AMapLocation;
import com.google.gson.reflect.TypeToken;
import com.gy.smart.MyApplication;
import com.gy.smart.R;
import com.gy.smart.base.BaseActivity;
import com.gy.smart.base.BaseBean;
import com.gy.smart.life.adapter.LifeListAdapter;
import com.gy.smart.life.bean.LifeListBean;
import com.gy.smart.life.bean.LifeTypeBean;
import com.gy.smart.life.view.LifeTitleView;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.StringConstants;
import com.gy.smart.util.Urls;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 17/1/10
 * 说明：生活服务列表
 */
public class LifeListActivity extends BaseActivity implements LifeTitleView.LifeTitleSelect{

    private LifeTitleView titleView;
    private PullToRefreshListView listView;
    private List<String> strTitles;
    private LifeListAdapter adapter;
    private List<LifeTypeBean> lifeCategoryList;
    private AMapLocation aMapLocation;//定位位置信息
    private List<LifeListBean> mDatas,requestList;
    private int page = 1,selectTitle;
    private String lc_id;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_life_list);
        setTitleMsg("生活服务列表");
        titleView = findView(R.id.activity_life_list_titleview);
        listView = findView(R.id.activity_life_list_refresh);
    }

    @Override
    protected void initData() {
        lifeCategoryList = (List<LifeTypeBean>) getIntent().getSerializableExtra("titles");
        strTitles = new ArrayList<>();
        if(lifeCategoryList != null){
            for(LifeTypeBean typeBean : lifeCategoryList){
                strTitles.add(typeBean.lc_name);
            }
        }
        requestList = new ArrayList<>();
        mDatas = new ArrayList<>();
        adapter = new LifeListAdapter(this,mDatas);
    }

    @Override
    protected void setListener() {
        super.setListener();
        titleView.setListener(this);
        titleView.setTitles(strTitles);
        listView.setMode(PullToRefreshBase.Mode.DISABLED);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(LifeListActivity.this,LifeDetailActivity.class);
                intent.putExtra("life_id",mDatas.get(position - 1).life_id);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(LifeListActivity.this);
            }
        });
        listView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if(requestList.size() == StringConstants.PAGENUM && !isLoading){
                    isLoading = true;
                    page++;
                    requestData(lc_id);
                }
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
        if(MyApplication.aMapLocation != null){
            this.aMapLocation = MyApplication.aMapLocation;
            requestData("-1");
        }
    }

    @Override
    public void lifeTitleSelect(int position) {
        if(position != selectTitle){
            selectTitle = position;
            mDatas.clear();
            requestData(lifeCategoryList.get(position - 1).lc_id);
            lc_id = lifeCategoryList.get(position - 1).lc_id;
        }
    }

    private void requestData(String id){
        showLoadDialog();
        RequestParams params = new RequestParams(Urls.url_life_list);
        if("-1".equals(id)){
            params.addParameter("lc_id","");
        } else {
            params.addParameter("lc_id",id+"");
        }
        params.addParameter("longitude",aMapLocation.getLongitude()+"");
        params.addParameter("latitude",aMapLocation.getLatitude()+"");
        params.addParameter("page",page+"");
        params.addParameter("pagenum",StringConstants.PAGENUM);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    Type type = new TypeToken<List<LifeListBean>>(){}.getType();
                    requestList = gson.fromJson(result.data,type);
                    mDatas.addAll(requestList);
                    adapter.notifyDataSetChanged();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
